<template>
<button @click="openBluetoothAdapter">开始扫描</button>
<button @click="stopBluetoothDevicesDiscovery">停止扫描</button>
<button @click="closeBluetoothAdapter">结束流程</button>

<view class="devices_summary">已发现 {{devices.length}} 个外围设备：</view>
<scroll-view class="device_list" scroll-y scroll-with-animation>
  <view wx:for="{{devices}}" wx:key="index"
   data-device-id="{{item.deviceId}}"
   data-name="{{item.name || item.localName}}"
   @click="createBLEConnection" 
   class="device_item"
   hover-class="device_item_hover">
    <view style="font-size: 16px; color: #333;">{{item.name}}</view>
    <view style="font-size: 10px">信号强度: {{item.RSSI}}dBm ({{max(0, item.RSSI + 100)}}%)</view>
    <view style="font-size: 10px">UUID: {{item.deviceId}}</view>
    <view style="font-size: 10px">Service数量: {{len(item.advertisServiceUUIDs)}}</view>
  </view>
</scroll-view>

<view class="connected_info" wx:if="{{connected}}">
  <view>
    <text>已连接到 {{name}}</text>
    <view class="operation">
    <button wx:if="{{canWrite}}" size="mini" @click="writeBLECharacteristicValue">写数据</button>
    <button size="mini" @click="closeBLEConnection">断开连接</button>
    </view>
  </view>
  <view wx:for="{{chs}}" wx:key="index" style="font-size: 12px; margin-top: 10px;">
    <view>特性UUID: {{item.uuid}}</view>
    <view>特性值: {{item.value}}</view>
  </view>
</view>
</template>

<script>
  export default {
    data() {
      return {
        // key: value
      }
    },
    methods: {
        max(n1, n2) {
        return Math.max(n1, n2)
        },
        len(arr) {
        arr = arr || []
        return arr.length
        },
      init() {
        let that = this
        if (wx.openBluetoothAdapter) {
          console.log('11111');
          
            wx.openBluetoothAdapter({
                success: function(res) {
                    /* 获取本机的蓝牙状态 */
          console.log('11');
                    setTimeout(() => {
                        that.getBluetoothAdapterState()
                    }, 1000)
                },
                fail: function(err) {
          console.log('110', err); 
                  // 初始化失败
                }
            })
        } else {
          console.log('10');
          this.$showToast('请打开蓝牙')
        }
      },
      getBluetoothAdapterState() {
          console.log('2');
          returns
          var that = this;
          // that.toastTitle = '检查蓝牙状态'
          wx.getBluetoothAdapterState({
              success: function(res) {
                startBluetoothDevicesDiscovery()
              },
              fail(res) {
                  console.log(res)
              }
          })
      },
      startBluetoothDevicesDiscovery() {
          console.log('3');
          var that = this;
          setTimeout(() => {
              wx.startBluetoothDevicesDiscovery({
                  success: function(res) {
                      /* 获取蓝牙设备列表 */
                      that.getBluetoothDevices()
                  },
                  fail(res) {
                  }
              })
          }, 1000)
      },

      getBluetoothDevices() {
          console.log('4');
        var that = this;
        setTimeout(() => {
            wx.getBluetoothDevices({
                services: [],
                allowDuplicatesKey: false,
                interval: 0,
                success: function(res) {
                    if (res.devices.length > 0) {
                        if (JSON.stringify(res.devices).indexOf(that.deviceName) !== -1) {
                            for (let i = 0; i < res.devices.length; i++) {
                                if (that.deviceName === res.devices[i].name) {
                                    /* 根据指定的蓝牙设备名称匹配到deviceId */
                                    that.deviceId = that.devices[i].deviceId;
                                    setTimeout(() => {
                                        that.connectTO();
                                    }, 2000);
                                };
                            };
                        } else {
                        }
                    } else {
                    }
                },
                fail(res) {
                    console.log(res, '获取蓝牙设备列表失败=====')
                }
            })
        }, 2000)
    },

    connectTO() {
          console.log('6');
        wx.createBLEConnection({
            deviceId: deviceId,
            success: function(res) {
                that.connectedDeviceId = deviceId;
                /* 4.获取连接设备的service服务 */
                that.getBLEDeviceServices();
                wx.stopBluetoothDevicesDiscovery({
                    success: function(res) {
                        console.log(res, '停止搜索')
                    },
                    fail(res) {
                    }
                })
            },
            fail: function(res) {
            }
        })
    },
    getBLEDeviceServices() {
          console.log('7');
        setTimeout(() => {
            wx.getBLEDeviceServices({
                deviceId: that.connectedDeviceId,
                success: function(res) {
                    that.services = res.services
                    /* 获取连接设备的所有特征值 */
                    that.getBLEDeviceCharacteristics()
                },
                fail: (res) => {
                }
            })
        }, 2000)
    },

    getBLEDeviceCharacteristics() {
          console.log('8');
        setTimeout(() => {
            wx.getBLEDeviceCharacteristics({
                deviceId: connectedDeviceId,
                serviceId: services[2].uuid,
                success: function(res) {
                    for (var i = 0; i < res.characteristics.length; i++) {
                        if ((res.characteristics[i].properties.notify || res.characteristics[i].properties.indicate) &&
                            (res.characteristics[i].properties.read && res.characteristics[i].properties.write)) {
                            console.log(res.characteristics[i].uuid, '蓝牙特征值 ==========')
                            /* 获取蓝牙特征值 */
                            that.notifyCharacteristicsId = res.characteristics[i].uuid
                            // 启用低功耗蓝牙设备特征值变化时的 notify 功能
                            that.notifyBLECharacteristicValueChange()
                        }
                    }
                },
                fail: function(res) {
                }
            })
        }, 1000)
    },

    notifyBLECharacteristicValueChange() { // 启用低功耗蓝牙设备特征值变化时的 notify 功能
          console.log('9');
            var that = this;
            console.log('6.启用低功耗蓝牙设备特征值变化时的 notify 功能')
            wx.notifyBLECharacteristicValueChange({
                state: true,
                deviceId: that.connectedDeviceId,
                serviceId: that.notifyServicweId,
                characteristicId: that.notifyCharacteristicsId,
                complete(res) {
                    /*用来监听手机蓝牙设备的数据变化*/
                    wx.onBLECharacteristicValueChange(function(res) {
                        /**/
                        that.balanceData += that.buf2string(res.value)
                        that.hexstr += that.receiveData(res.value)
                    })
                },
                fail(res) {
                    console.log(res, '启用低功耗蓝牙设备监听失败')
                    that.measuringTip(res)
                }
            })
        },
        
        /*转换成需要的格式*/
         buf2string(buffer) {
                    var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
                    return arr.map((char, i) => {
                        return String.fromCharCode(char);
                    }).join('');
        },
          receiveData(buf) {
                    return  this.hexCharCodeToStr(this.ab2hex(buf))
          },
          /*转成二进制*/
           ab2hex (buffer) {
              var hexArr = Array.prototype.map.call(
                  new Uint8Array(buffer), function (bit) {
                      return ('00' + bit.toString(16)).slice(-2)
                  }
              )
              return hexArr.join('')
          },
          /*转成可展会的文字*/
          hexCharCodeToStr(hexCharCodeStr) {
              var trimedStr = hexCharCodeStr.trim();
              var rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
              var len = rawStr.length;
              var curCharCode;
              var resultStr = [];
              for (var i = 0; i < len; i = i + 2) {
                  curCharCode = parseInt(rawStr.substr(i, 2), 16);
                  resultStr.push(String.fromCharCode(curCharCode));
              }
              return resultStr.join('');
          },

          sendData(str) {
          console.log('111');
            let that = this;
            let dataBuffer = new ArrayBuffer(str.length)
            let dataView = new DataView(dataBuffer)
            for (var i = 0; i < str.length; i++) {
                dataView.setUint8(i, str.charAt(i).charCodeAt())
            }
            let dataHex = that.ab2hex(dataBuffer);
            this.writeDatas = that.hexCharCodeToStr(dataHex);
            wx.writeBLECharacteristicValue({
                deviceId: that.connectedDeviceId,
                serviceId: that.notifyServicweId,
                characteristicId: that.notifyCharacteristicsId,
                value: dataBuffer,
                success: function (res) {
                    console.log('发送的数据：' + that.writeDatas)
                    console.log('message发送成功')
                },
                fail: function (res) {
                },
                complete: function (res) {
                }
            })
            },

        // 断开设备连接
        closeConnect() {
            if (that.connectedDeviceId) {
                wx.closeBLEConnection({
                    deviceId: that.connectedDeviceId,
                    success: function(res) {
                        that.closeBluetoothAdapter()
                    },
                    fail(res) {
                    }
                })
            } else {
                that.closeBluetoothAdapter()
            }
        },
        // 关闭蓝牙模块
        closeBluetoothAdapter() {
            wx.closeBluetoothAdapter({
                success: function(res) {
                },
                fail: function(err) {
                }
            })
        },

    },
    mounted() {
      this.init()
    },
    destroyed() {
      console.log('...destroy');
      this.closeBLEConnection()
      this.closeBluetoothAdapter()
    }
  }
</script>

<style scoped>